<template>
  <div>
  <Tabs size="small" @on-click="handlTabsNei">
    <!-- <TabPane label="常用事项">
      <Table style="margin: 1px" height="360" :columns="columns1" :data="data2"></Table>
    </TabPane> -->
    <TabPane :label="item.name" v-for="(item, index) in itemlist" v-bind:key="index" :name="'' + index" :index="index">
      <Table style="margin: 1px" height="360" :columns="columns2" :data="tablelist"></Table>
    </TabPane>
  </Tabs>
  <Modal v-model="modal1" :title="modalTitle" width="600" :closable="false">
    <Form label-position="right" :label-width="100" style="position: relative;">
      <Row class="mb20">
        <Col span="4" class="ivu-form-item-label">户籍情况：</Col>
        <Col span="6" class="ivu-form-item-label" v-if="registerFlag == '0'" style="text-align: center">{{register}}</Col>
        <Col span="6" class="ivu-form-item-label" v-if="registerFlag != '0'" style="text-align: center;color: red">{{register}}</Col>
        <Col span="4" class="ivu-form-item-label">低保情况：</Col>
        <Col span="6" class="ivu-form-item-label" v-if="lowInsuredFlag == '9999'" style="text-align: center;color: red">{{lowInsured}}</Col>
        <Col span="6" class="ivu-form-item-label" v-if="lowInsuredFlag != '9999'" style="text-align: center">{{lowInsured}}</Col>
      </Row>
      <Row class="mb20">
        <Col span="4" class="ivu-form-item-label">残疾情况：</Col>
        <Col span="6" class="ivu-form-item-label" v-if="disabledPersonFlag == 0" style="text-align: center; color:red">{{disabledLevel}}{{disabledPerson}}</Col>
        <Col span="6" class="ivu-form-item-label" v-if="disabledPersonFlag != 0" style="text-align: center">{{disabledLevel}}{{disabledPerson}}</Col>
        <Col span="4" class="ivu-form-item-label">殡葬情况：</Col>
        <Col span="8" class="ivu-form-item-label" v-if="funeralPersonnelFlag == 'true'" style="text-align: center;color:red">{{funeralPersonnel}}</Col>
        <Col span="7" class="ivu-form-item-label" v-if="funeralPersonnelFlag == 'false'">{{funeralPersonnel}}</Col>
      </Row>
    </Form>
    <div slot="footer">
      <Button type="success" @click="saveInfo()">办理</Button>
      <Button type="error" @click="modal1=false">关闭</Button>
    </div>
  </Modal>
  </div>
</template>

<script>
import { itemtodolist, yiBaoToken, getAssistanceInfo, findCanlian, getRegisterInfo, getFuneralInfo } from '@/api/eventlist'
export default {
  name: 'MyTabs',
  props: {
    loaduser: Boolean,
    searchname: String,
    usernname: String,
    cardno: String
  },
  data () {
    return {
      itemInfo: {},
      modalTitle: '基本情况',
      modal1: false,
      registerFlag: '',
      register: '',
      disabledPersonFlag: '',
      disabledPerson: '',
      disabledLevel: '',
      lowInsuredFlag: '',
      lowInsured: '',
      funeralPersonnelFlag: '',
      funeralPersonnel: '',
      itemlist: [],
      tablelist: [],
      item_index: 0,
      columns2: [
        { title: '事项名称', key: 'text', width: 1000 },
        // { title: '级别', key: 'type_name', align: 'center', width: 150 },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          className: 'nopadding',
          render: (h, params) => {
            let temp = []
            temp.push(
              h('Button', {
                props: { type: 'success', size: 'small' },
                style: { marginLeft: '5px' },
                on: {
                  click: () => {
                    this.todo(params.row)
                  }
                }
              }, '办理')
            )
            if (params.row.id === '11210101000000MZJD5212000330000') {
              temp.push(
                h('Button', {
                  props: { type: 'info', size: 'small' },
                  style: { marginLeft: '5px' },
                  on: {
                    click: () => {
                      this.stodo(params.row)
                    }
                  }
                }, '收受分离')
              )
            }
            if (params.row.ORG_ID === '12210100738669464Y') {
              temp.push(
                h('Button', {
                  props: { type: 'info', size: 'small' },
                  style: { marginLeft: '5px' },
                  on: {
                    click: () => {
                      this.btodo(params.row)
                    }
                  }
                }, '办结成功')
              )
            }
            return h('div', temp)
          }
        }
      ]
    }
  },
  methods: {
    // 切换表格
    handlTabsNei (index) {
      this.item_index = index
      this.filterData()
    },
    // 根据搜索框内关键字筛选事项列表
    filterData () {
      let datalist = this.itemlist[this.item_index].datalist || []
      this.tablelist = []
      if (this.searchname) {
        datalist.forEach(element => {
          if (element.text.indexOf(this.searchname) >= 0) {
            this.tablelist.push(element)
          }
        })
      } else {
        this.tablelist = datalist
      }
    },
    todo (item) {
      if (this.loaduser) {
        if (item.type_id === '12210100738669464Y') {
          yiBaoToken({}).then(ret => {
            if (ret.data.data) {
              window.open(ret.data.data.url, '_blank')
            } else {
              this.$Notice.error({ title: '错误', desc: '获取链接失败，请重试' })
            }
          })
        } else if (item.id === '11210101000000MZJD5212000330000') {
          this.$router.push({
            name: 'subsistenceapplication',
            params: {
              cardno: this.cardno,
              name: this.usernname,
              operation: '1'
            }
          })
        } else if (item.id === '11210101000000MZJD5212000328000') {
          this.$router.push({
            name: 'transregionalmigration',
            params: {
              cardno: this.cardno,
              operation: '1'
            }
          })
        } else if (item.id === '11210101000000MZJD5212000173000') {
          this.$router.push({
            name: 'stopinsurance',
            params: {
              cardno: this.cardno,
              operation: '1'
            }
          })
        } else if (item.type_id === '11210101000000FCJD') {
          this.$router.push({
            name: 'house_handinformation',
            params: {
              itemid: item.id,
              cardno: this.cardno,
              operation: '1',
              interface_code: item.interface_code
            }
          })
        } else if (item.id === '11210101000000MZJD5212000207000' || item.id === '11210101000000MZJD5212000216000') {
          // 调用对接接口
          console.log('调用对接接口')
          this.itemInfo = item
          this.getHouseholdRegister()
          this.getDisablePerson(item.id)
          this.getLowPerson()
          this.getFuneral()
          this.modal1 = true
        } else {
          this.$router.push({
            name: 'handinformation',
            params: {
              itemid: item.id,
              cardno: this.cardno,
              operation: '1'
            }
          })
        }
      } else {
        this.$Notice.warning({ title: '无用户信息', desc: '请先获取用户信息后再办理事项' })
      }
    },
    stodo (item) {
      if (this.loaduser) {
        let itemid = item.id + ''
        if (item.type_id === '12210100738669464Y') {
          yiBaoToken({}).then(ret => {
            if (ret.data.data) {
              window.open(ret.data.data.url, '_blank')
            } else {
              this.$Notice.error({ title: '错误', desc: '获取链接失败，请重试' })
            }
          })
        } else if (itemid === '11210101000000MZJD5212000330000') {
          this.$router.push({
            name: 'handinformation',
            params: {
              itemid: '372510',
              cardno: this.cardno,
              operation: '1'
            }
          })
        }
      } else {
        this.$Notice.warning({ title: '无用户信息', desc: '请先获取用户信息后再办理事项' })
      }
    },
    btodo (item) {
      if (this.loaduser) {
        let itemid = item.id + ''
        if (item.type_id === '12210100738669464Y') {
          yiBaoToken({}).then(ret => {
            if (ret.data.data) {
              this.$router.push({
                name: 'handinformation',
                params: {
                  itemid: itemid,
                  cardno: this.cardno,
                  operation: '1'
                }
              })
            } else {
              this.$Notice.error({ title: '错误', desc: '获取链接失败，请重试' })
            }
          })
        }
      } else {
        this.$Notice.warning({ title: '无用户信息', desc: '请先获取用户信息后再办理事项' })
      }
    },
    // 低保对象过滤
    formatLowPerson (val) {
      return val === '1010' ? '城市低保对象' : val === '1020' ? '城市边缘户对象' : val === '1030' ? '农村低保对象' : val === '1040' ? '农村边缘户对象' : val === '9999' ? '非保障对象' : ''
    },
    // 查询户籍情况
    getHouseholdRegister () {
      var param = { 'IDcard': this.cardno, 'Name': this.usernname }
      getRegisterInfo(param).then(ret => {
        console.log(ret)
        if (ret.data.errcode === 0) {
          this.registerFlag = ret.data.data.result
          if (ret.data.data.result === '-1') {
            this.register = '非沈阳户籍地'
          }
          if (ret.data.data.result === '0') {
            this.register = '沈阳有效户籍'
          }
          if (ret.data.data.result === '1') {
            this.register = '户籍已删除'
          }
          if (ret.data.data.result === '2') {
            this.register = '户籍被加锁'
          }
          if (ret.data.data.result === '3') {
            this.register = '重错号'
          }
          if (ret.data.data.result === '4') {
            this.register = '跨市重错号'
          }
          if (ret.data.data.result === '6') {
            this.register = '持证'
          }
          if (ret.data.data.result === '7') {
            this.register = '在逃犯'
          }
          if (ret.data.data.result === '8') {
            this.register = '户籍已迁出沈阳'
          }
          if (ret.data.data.result === '9') {
            this.register = '死亡'
          }

          return
        }
        this.$Modal.error({
          title: '错误',
          content: ret.data.errmsg
        })
      })
    },
    // 查询低保情况
    getLowPerson (cardno) {
      var param1 = {
        'azcp0001': this.cardno,
        'idcardNumber': this.cardno,
        'serialNumber': '123456'
      }
      getAssistanceInfo(param1).then(ret => {
        if (ret.data.errcode === 0) {
          if (ret.data.data.rescode === '0000') {
            this.lowInsuredFlag = ret.data.data.data.type
            console.log(this.lowInsuredFlag, 'this.lowInsuredFlag')
            this.lowInsured = this.formatLowPerson(ret.data.data.data.type)
            console.log(this.lowInsured)
            return
          }
          this.$Modal.error({
            title: '错误',
            content: ret.data.data.memo
            /* onOk: () => {
             this.modal1 = false
             } */
          })
          return
        }
        this.$Modal.error({
          title: '错误',
          content: ret.data.errmsg,
          onOk: () => {
            this.modal1 = false
          }
        })
      })
    },
    // 查询残疾情况
    getDisablePerson (itemId) {
      var param2 = {
        cardno: this.cardno
      }
      findCanlian(param2).then(ret => {
        if (ret.data.errcode === 0) {
          console.log(ret.data.data, '残疾人详情')
          this.disabledPersonFlag = ret.data.data.length
          console.log(this.disabledPersonFlag, 'this.disabledPersonFlag')
          if (ret.data.data.length === 0) {
            this.disabledPerson = '非残疾人'
            this.disabledLevel = ''
          } else if (itemId === '11210101000000MZJD5212000216000') {
            console.log('重度残疾人')
            this.disabledPerson = ret.data.data[0].DISABLED_TYPE_NAME
            if (ret.data.data[0].DISABLED_LEVEL !== '一级' && ret.data.data[0].DISABLED_LEVEL !== '二级') {
              this.disabledLevel = '非一，二级'
            } else {
              this.disabledLevel = ret.data.data[0].DISABLED_LEVEL
            }
          } else {
            this.disabledPerson = ret.data.data[0].DISABLED_TYPE_NAME
            this.disabledLevel = ret.data.data[0].DISABLED_LEVEL
          }
          return
        }
        this.$Modal.error({
          title: '错误',
          content: ret.data.errmsg,
          onOk: () => {
          }
        })
      })
    },
    // 查询殡葬
    getFuneral () {
      var param1 = {
        'idCode': this.cardno
      }
      getFuneralInfo(param1).then(ret => {
        if (ret.data.errcode === 0) {
          this.funeralPersonnelFlag = ret.data.data.flag
          console.log(this.funeralPersonnelFlag, 'this.funeralPersonnelFlag')
          if (ret.data.data.flag === 'false') {
            this.funeralPersonnel = '未查询到该人员殡葬信息'
            console.log(this.lowInsured)
            return
          }
          if (ret.data.data.flag === 'true') {
            this.funeralPersonnel = '登记死亡人员,请精神操作'
            return
          }
          this.$Modal.error({
            title: '错误',
            content: ret.data.data.memo
            /* onOk: () => {
              this.modal1 = false
              } */
          })
          return
        }
        this.$Modal.error({
          title: '错误',
          content: ret.data.errmsg,
          onOk: () => {
            this.modal1 = false
          }
        })
      })
    },
    saveInfo () {
      console.log('~~~~~')
      this.$router.push({
        name: 'handinformation',
        params: {
          itemid: this.itemInfo.id,
          cardno: this.cardno,
          operation: '1'
        }
      })
    }
  },
  watch: {
    searchname (data) {
      this.filterData()
    }
  },
  mounted () {
    itemtodolist().then(ret => {
      this.itemlist = ret.data.data
      this.tablelist = this.itemlist[0].datalist || []
      // this.$forceUpdate()
    })
  }
}
</script>
